<?php

class Ability extends Model {

    public $type;
    public $title;
    public $tooltip;
    public $champion;

    public static $shortcuts = array(
            0 => 'M1',
            1 => 'M2',
            2 => 'SP',
            3 => 'Q',
            4 => 'E',
            5 => 'R',
            6 => 'F',
            7 => 'EX1',
            8 => 'EX2'
    );

    public function __construct($container, $params = array()) {

        parent::__construct($container, $params);

        if (isset($this->index))
            $this->shortcut = self::$shortcuts[$this->index];
    }
    

    public function save() {

        $sql = "insert into av_bloodlines_abilities (bloodline_id, name, type, tooltip, specs, shortcut, icon)
                values (
                '" . $this->champion->id . "',
                '" . addslashes(stripslashes($this->name)) . "',
                '" . addslashes(stripslashes($this->type)) . "',
                '" . addslashes(stripslashes($this->tooltip)) . "',
                '" . addslashes(stripslashes($this->specs)) . "',
                '" . addslashes(stripslashes($this->shortcut)) . "',
                '" . addslashes(stripslashes($this->icon)) . "'
                )";

        $this->id = $this->db->query($sql);
    }

    public static function get_by_shortcut($container, $champion, $shortcut) {

        $sql = "
            select 
            b.*
            from av_bloodlines_abilities b 
            where 
            b.shortcut = '" . $shortcut . "'
            and b.bloodline_id = '".$champion->id."'
            "
        ;

        $index = $container->db->query($sql);

        if ($record = $container->db->get_record($index)) {

            return new self($container, $record);
        }

        return null;
    }

    public static function get_by_name($container, $name) {

        $sql = "
            select 
            b.*
            from av_bloodlines_abilities b 
            where 
            b.name = '" . addslashes(stripslashes($name)) . "'"
        ;

        $index = $container->db->query($sql);

        if ($record = $container->db->get_record($index)) {

            return new self($container, $record);
        }

        return null;
    }

}